﻿using Orchard.ContentManagement;
using Orchard.Core.Common.Models;
using Orchard.Core.Title.Models;
using Vitus.Featured.Settings;

namespace Vitus.Featured.Models
{
    public class FeaturedPart : ContentPart<FeaturedPartRecord>
    {
        public bool IsFeatured
        {
            get { return this.Record.IsFeatured; }
            set { this.Record.IsFeatured = value; }
        }

        public string Title
        {
            get
            {
                var settings = this.Settings.GetModel<FeaturedSettings>();

                switch (settings.TitlePartUsageMode)
                {
                    case FieldDependencyMode.UseAlways:
                        return this.Is<TitlePart>() ? this.As<TitlePart>().Title : null;

                    case FieldDependencyMode.Disallow:
                        return this.Record.Title;

                    default:
                        return string.IsNullOrEmpty(this.Record.Title) && this.Is<TitlePart>() ? this.As<TitlePart>().Title : this.Record.Title;
                }
            }
            set { this.Record.Title = value; }
        }

        public string Body
        {
            get
            {
                var settings = this.Settings.GetModel<FeaturedSettings>();

                switch (settings.BodyPartUsageMode)
                {
                    case FieldDependencyMode.UseAlways:
                        return this.Is<BodyPart>() ? this.As<BodyPart>().Text : null;

                    case FieldDependencyMode.Disallow:
                        return this.Record.Body;

                    default:
                        return string.IsNullOrEmpty(this.Record.Body) && this.Is<BodyPart>() ? this.As<BodyPart>().Text : this.Record.Body;
                }
            }
            set { this.Record.Body = value; }
        }

        public bool IsPinned
        {
            get { return this.Record.IsPinned; }
            set { this.Record.IsPinned = value; }
        }
    }
}
